package com.javasm.demobike;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;

public abstract  class ShareBikeCompany implements ShareBikeDao {
    //单车公司名字
    private String bikeCompanyName;
    //单车公司ID
    private int bikeCompanyId;
    //公司可持有的共享单车
    private ShareBike[] shareBikes;
    //单车公司现有的总量
    private int sum;
    //公司单车借出去的次数
    private int count;

    //生成单车ID的变量
    private int bikeId = 1;

    public ShareBikeCompany(String bikeCompanyName, int bikeCompanyId, ShareBike[] shareBikes, int sum, int count, int bikeId) {
        this.bikeCompanyName = bikeCompanyName;
        this.bikeCompanyId = bikeCompanyId;
        this.shareBikes = shareBikes;
        this.sum = sum;
        this.count = count;
        this.bikeId = bikeId;
    }


    public String getBikeCompanyName() {
        return bikeCompanyName;
    }

    public void setBikeCompanyName(String bikeCompanyName) {
        this.bikeCompanyName = bikeCompanyName;
    }

    public int getBikeCompanyId() {
        return bikeCompanyId;
    }

    public void setBikeCompanyId(int bikeCompanyId) {
        this.bikeCompanyId = bikeCompanyId;
    }

    public ShareBike[] getShareBikes() {
        return shareBikes;
    }

    public void setShareBikes(ShareBike[] shareBikes) {
        this.shareBikes = shareBikes;
    }

    public int getSum() {
        return sum;
    }

    public void setSum(int sum) {
        this.sum = sum;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getBikeId() {
        return bikeId;
    }

    public void setBikeId(int bikeId) {
        this.bikeId = bikeId;
    }

    public ShareBikeCompany(String bikeCompanyName, Integer bikeCompanyId, ShareBike[] shareBikes, Integer sum, Integer count) {
        this.bikeCompanyName = bikeCompanyName;
        this.bikeCompanyId = bikeCompanyId;
        this.shareBikes = shareBikes;
        this.sum = sum;
        this.count = count;
    }

    public ShareBikeCompany() {
    }

    @Override
    public void addBike(int num) {
        if((sum+num) > shareBikes.length){
            shareBikes = Arrays.copyOf(shareBikes,(shareBikes.length+ num )*5+1);
            System.out.println("扩容后的容量是" + shareBikes.length);
        }



        for(int i=0; i<num ;i++){
            shareBikes[sum] = new ShareBike(bikeId++,0,null);
            sum++;
        }
        System.out.println("投放成功了！牛逼，你小子就是一个天才！");


    }

    @Override
    public void queryBike() {

    }

    @Override
    public void DeleteBike(int bikeId) {
        for (int i = 0; i < sum; i++) {
            ShareBike shareBike = shareBikes[i];
            if(shareBike.getBid() == bikeId){
                if(shareBike.getStatus() == 1){
                    for (int j = i;  j<sum - 1; j++){
                        shareBikes[j] = shareBikes[j+1];
                    }
                    //实际个数减1
                    sum--;
                    shareBikes[sum] = null;
                    System.out.println("删除成功了，你真的是强啊大兄弟！");

                }else {
                    System.out.println("这辆车正在租借状态，不能删除啊兄弟~！");
                }

                return;

            }



        }
        System.out.println("真的没有找到你要找到id啊，我该怎么办？你在想想办法把");

    }

    @Override
    public void BorrowBike(int num) {
        for (int i = 0; i < sum; i++) {
            ShareBike shareBike = shareBikes[i];
           if(shareBike.getBid() == num){
               //找到了对应的单车
               //判断是否可借
               if(shareBike.getStatus() == 1){
                   shareBike.setStatus(0);
                   shareBike.setBorrowTime(new Date());
                   System.out.println("借出成功了，你小子真幸运！");
                   count++;
               }else {
                   System.out.println("此辆车已经被人借出了，请你换一辆车借把");
               }
               return;
           }
        }
        System.out.println("没有你说的ID的车，你是不是整错了，小伙子，再仔细看看");
    }

    @Override
    public void BackBike(int bikeId) {
        for (int i = 0; i < sum; i++) {
            ShareBike shareBike = shareBikes[i];
            if(shareBike.getBid() == bikeId){
                if(shareBike.getStatus() == 0){
                    Date backDate = new Date();
                    //获取这个时间到计算机元年的毫秒值
                    long time = backDate.getTime();
                    System.out.println(time);

                    Date borrowDate = shareBike.getBorrowTime();
                    long time2 = borrowDate.getTime();
                    System.out.println(time2);

                    long totalTime = (time - time2)/1000;
//                    //转换成时间日期格式
//                    Date date = new Date(totalTime);
//                    DateFormat df = new SimpleDateFormat("mm-ss");
//                    String str = df.format(date);
                    System.out.println("找到了对应要换的车,已经成功归还!");
                    System.out.println("你一共借了"+ totalTime+"秒" +"的车，赶紧休息一下吧，累成狗了，恭喜你！");
                    System.out.println("你要交的钱是" + totalTime * 0.55 +"元，请支付！");
                    shareBike.setStatus(1);
                    shareBike.setBorrowTime(null);

                    count--;
                } else if (shareBike.getStatus() == 1) {
                    System.out.println("请重新输入ID，这个车是没有借出的状态，还个鸡毛啊还？");
                    return;
                }
            }
        }
        System.out.println("没有找到对应的ID，你睁大你的狗眼看看你到底是怎么回事？");
    }
}
